package cn.xz.zysjz.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.WindowManager;
import java.util.ArrayList;
import cn.xz.zysjz.R;
import cn.xz.zysjz.bean.CommonLib;
import cn.xz.zysjz.bean.HomeListBean;
import cn.xz.zysjz.bean.IndexBean;
import cn.xz.zysjz.databinding.ActivityAddYactivityBinding;
import cn.xz.zysjz.utils.AppHelper;
import cn.xz.zysjz.utils.SessionUtils;
import cn.xz.zysjz.utils.ToastUtils;
import cn.xz.zysjz.utils.UpLoadUtils;


/**
 * 添加内容 */
public class AddYActivity extends AppCompatActivity {

    ActivityAddYactivityBinding mBinding;

    private int mTK = 10010; //相册返回码

    private String mUrl = ""; //图片地址

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_yactivity);
        if (Build.VERSION.SDK_INT >= 21) {
            this.getWindow()
                    .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        initEvent();
    }

    private void initEvent(){
        //返回
        mBinding.viewBack.setOnClickListener(v->{
            finish();
        });
        //点击头像
        mBinding.cvHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开相册
                Intent intent  = new Intent(Intent.ACTION_PICK);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, mTK);
            }
        });
        //添加
        mBinding.tvDl.setOnClickListener(v->{
            //名称
            String dd = mBinding.etDd.getText().toString();
            //热度
            String ys = mBinding.etYs.getText().toString();
            //说明
            String sm = mBinding.etSm.getText().toString();

            if (dd.equals("") || ys.equals("") || mUrl.equals("")){
                ToastUtils.show("请输入正确内容");
                return;
            }

            ArrayList<HomeListBean> list = SessionUtils.getList(CommonLib.ADD_YS, HomeListBean.class);
            HomeListBean indexBean = new HomeListBean();
            //添加昵称
            indexBean.setMsg(dd);
            //评分
            indexBean.setGet(Integer.parseInt(ys));
            //图片地址
            indexBean.setUrl(mUrl);
            ///说明
            indexBean.setText(sm);
            //编号
            indexBean.setNum(60 + list.size() + 6);
            //类型
            int type = 1;
            //景点
            if (mBinding.t1.isChecked()){
                type = 1;
            }
            //酒店
            if (mBinding.t2.isChecked()){
                type = 3;
            }
            //美食
            if (mBinding.t3.isChecked()){
                type = 2;
            }
            //民宿
            if (mBinding.t4.isChecked()){
                type = 4;
            }
            indexBean.setType(type);
            //是否免费
            boolean free = false;
            if (mBinding.rb1.isChecked()){
                free = true;
            }
            indexBean.setFree(free);
            list.add(indexBean);
            SessionUtils.commitList(CommonLib.ADD_YS,list);
            ToastUtils.show("添加成功");
            finish();
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == mTK){
            if (data != null){
                UpLoadUtils.to(data.getData(), new UpLoadUtils.PutOk() {
                    @Override
                    public void click(String s) {
                        mBinding.ivHead.post(new Runnable() {
                            @Override
                            public void run() {
                                mUrl = s;
                                AppHelper.setLocalPic(mBinding.ivHead,mUrl);
                            }
                        });
                    }
                });
            }
        }
    }
}